package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.exception.OrderFoodException;
import com.woniuxy.commons.exception.code.OrderFoodExceptionCode;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.controller.form.CreateOrderFoodForm;
import com.woniuxy.dao.model.DietOrderFood;
import com.woniuxy.dao.DietOrderFoodDao;
import com.woniuxy.service.DietOrderFoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.dto.FoodMonthStacDto;
import com.woniuxy.service.dto.UserDetailsDto;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class DietOrderFoodServiceImpl extends ServiceImpl<DietOrderFoodDao, DietOrderFood> implements DietOrderFoodService {

    @Resource
    private DietOrderFoodService dietOrderFoodService;

    @Resource
    private DietOrderFoodDao dietOrderFoodDao;

    @Override
    public boolean addOrderFood(CreateOrderFoodForm createOrderFoodForm) {
        DietOrderFood dietOrderFood = BeanCopyUtil.copyOne(createOrderFoodForm, DietOrderFood.class);
        boolean save = dietOrderFoodService.save(dietOrderFood);
        if(save){
            return true;
        }else{
            throw new OrderFoodException(OrderFoodExceptionCode.ORDERFOOD_ADD_FAIL.getCode(),OrderFoodExceptionCode.ORDERFOOD_ADD_FAIL.getMessage());
        }
    }

    @Override
    public Page<FoodMonthStacDto> getFoodStac(Long pageNum,Long pageSize) {
        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Long storeId = userDetailsDto.getId();

        Page<DietOrderFood> foodStac = dietOrderFoodDao.getFoodStac(new Page<DietOrderFood>(pageNum, pageSize),storeId);

        Page<FoodMonthStacDto> foodMonthStacDtoPage = BeanCopyUtil.copyPageInfo(foodStac, FoodMonthStacDto.class);

        return foodMonthStacDtoPage;
    }
}
